package com.ego.portal.service.impl;

import com.ego.commons.pojo.BigAd;
import com.ego.commons.utils.JsonUtils;
import com.ego.dubbo.service.TBContentDubboService;
import com.ego.pojo.TbContent;
import com.ego.portal.service.PortalService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PortalServiceImpl implements PortalService {

    @DubboReference
    private TBContentDubboService tbContentDubboService;


    @Value("${ego.bigad.categoryId}")
    private long egoBigadCategoryId;

    @Cacheable(cacheNames = "com.ego.portal",key = "'bigad'")
    public String selectBigAd() {

        List<TbContent> tbContents = tbContentDubboService.selectTbContentsByCategoryId(egoBigadCategoryId);

        List<BigAd> bigAds = new ArrayList<>();
        //循环大广告集合类容，将TbContent数据获取出来封装成一个个 BigAd 对象
        for (TbContent tbContent : tbContents) {

            BigAd bigAd = new BigAd();
            bigAd.setWidth(670);
            bigAd.setHeight(240);
            bigAd.setSrc(tbContent.getPic());
            bigAd.setSrcB(tbContent.getPic2());
            bigAd.setHref(tbContent.getUrl());
            bigAd.setWidthB(550);
            bigAd.setHeightB(240);
            bigAd.setAlt("");

            //将广告对象添加到集合中
            bigAds.add(bigAd);
        }

        return JsonUtils.objectToJson(bigAds);
    }
}
